package com.sanploy.file.config;



import com.sanploy.file.util.FileUtil;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by  on zhu  静态资源配置
 */
@Configuration
@ComponentScan
public class WebResourceConfig extends WebMvcConfigurerAdapter {
    /**
     * {@inheritDoc}
     * <p>This implementation is empty.
     * 配置默认静态资源路径为与项目运行的同级目录之下
     * @param registry
     */


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        String s = FileUtil.getJarDir();
        String foldname = "";
        System.out.println("s: "+s);
        if (s.contains(".jar")){ //是否为jar包
            File file = new File(s);
            System.out.println("file.getAbsolutePath(): "+file.getAbsolutePath());
            foldname = file.getParentFile().getParent()+File.separator+"file"+File.separator;
        }else {
            foldname = s+File.separator+"file"+File.separator; //构建目录
        }

        System.out.println("foldname:"+foldname);
        FileUtil.pathIsExist(foldname);
        FileUtil.scanFirstFold(foldname);

        if(foldname.contains("file:")){//foldname需要加上file:
            registry.addResourceHandler("/**").
                    addResourceLocations(foldname);
        }else {
            registry.addResourceHandler("/**").
                    addResourceLocations("file:"+foldname);
        }
    }

}
